# 前端构建阶段
FROM node:18 as frontend-builder

WORKDIR /usr/src/app
COPY ./chat-master-web /usr/src/app
RUN npm install
RUN npm install whatwg-fetch --save
RUN npm run build

# 管理端构建阶段
FROM node:14 as admin-builder

WORKDIR /usr/src/app
COPY ./chat-master-admin /usr/src/app
RUN npm install
RUN npm run build:prod

# 后端构建阶段
FROM maven:3.8-openjdk-8 as backend-builder

WORKDIR /usr/src/app

COPY ./chat-master-server /usr/src/app
RUN mvn clean package -DskipTests

# 最终阶段 - 使用Caddy提供静态文件服务和代理API请求
FROM caddy:2.8.4-alpine

RUN apk add --no-cache openjdk8

# 复制前端和管理端的构建产物到Caddy的默认服务目录
COPY --from=frontend-builder /usr/src/app/dist /usr/share/caddy
COPY --from=admin-builder /usr/src/app/dist /usr/share/caddy/admin

# 使用变量 JAR_FILE 复制后端构建产物
ARG JAR_FILE
COPY --from=backend-builder /usr/src/app/chat-*/target/${JAR_FILE} /usr/share/caddy/app.jar
# 复制自定义的Caddyfile配置文件到容器中的相应位置
COPY Caddyfile /etc/caddy/Caddyfile

EXPOSE 80
EXPOSE 443
EXPOSE 8088

# 启动Caddy和后端服务
CMD ["sh", "-c", "java -jar /usr/share/caddy/app.jar & caddy run --config /etc/caddy/Caddyfile"]
